package com.syyo.gulimall.order.controller;

import com.syyo.gulimall.order.domain.entity.OrderEntity;
import com.syyo.gulimall.order.domain.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @ author: wang
 * @ date: 2021/4/22 14:41
 * @ Description:
 */
@Slf4j
@RestController
public class TestRabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;// 管理队列对象


    private static String EXCHANGE = "hello-direct-exchange";
    private static String QUEUE = "hello-queue";
    private static String KEY = "hello";

    @GetMapping("/mq")
    public void send() {
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                OrderReturnReasonEntity entity = new OrderReturnReasonEntity();
                entity.setCreateTime(new Date());
                entity.setId(1L);
                entity.setName("哈哈" + i);
                // 如果发送的消息是个对象，我们会使用序列化机制，将对象写出去，对象必须实现Serializable
                String msg = "nihao";
                rabbitTemplate.convertAndSend(EXCHANGE, KEY, entity, new CorrelationData(UUID.randomUUID().toString()));
//                System.out.println("=================");
//                System.out.println(entity);
            } else {
                OrderEntity entity = new OrderEntity();
                entity.setOrderSn(UUID.randomUUID().toString());
                // 如果发送的消息是个对象，我们会使用序列化机制，将对象写出去，对象必须实现Serializable
                rabbitTemplate.convertAndSend(EXCHANGE, "hello22", entity, new CorrelationData(UUID.randomUUID().toString()));
//                System.out.println("----------------------");
//                System.out.println(entity);
            }
        }
    }


}
